﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace NetFrame.Tool
{
    [Serializable]
    public sealed class AppSetting
    {
        public Dictionary<string, string> Settings=new Dictionary<string, string>();

        [NonSerialized]
        public static AppSetting Ins;

        [NonSerialized]
        public static string settingPath = AppDomain.CurrentDomain.BaseDirectory+"/"+ "AppSetting.json";
        

        static AppSetting() {
            try {
                using (StreamReader sr = new StreamReader(settingPath)) {
                    string content = sr.ReadToEnd();
                    Ins = JsonConvert.DeserializeObject<AppSetting>(content);
                }
            }
            catch (Exception ex) {
                Console.WriteLine(ex.ToString());
            }
        }


        public static string GetValue(string name) {
            if (Ins == null) {
                InitSetting();
            }

            if (Ins.Settings.ContainsKey(name)) {
                return Ins.Settings[name];
            }
            else {

                return string.Empty;
            }
        }

        static void InitSetting() {
            try {
                using (StreamReader sr = new StreamReader(settingPath)) {
                    string content = sr.ReadToEnd();
                    Console.WriteLine(content);
                    Ins = JsonConvert.DeserializeObject<AppSetting>(content);
                }
            }
            catch (Exception ex) {
                Console.WriteLine(ex.ToString());
            }
        }
    }
    
}
